package com.example.joke.utils

import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import kotlin.math.roundToInt

/**
 * @author：yangliu
 * @date：2024/07/03 11:43
 * @description：kotlin扩展函数
 **/

/**
 * dp转换px的扩展函数
 */
val Int.dp
  get() = (DensityUtil.getDensity() * this).roundToInt()

/**
 * dp转换px的扩展函数
 */
val Double.dp
  get() = (DensityUtil.getDensity() * this).roundToInt()

fun View?.visible() {
  this?.visibility = View.VISIBLE
}

//fun View?.visible(visible: Boolean) {
//  this?.visibility = if (visible) View.VISIBLE else View.GONE
//}

fun View?.gone() {
  this?.visibility = View.GONE
}

fun View?.invisible() {
  this?.visibility = View.INVISIBLE
}

val dinBold: Typeface = Typeface.createFromAsset(Utils.getAssets(), "din_bold.otf")

/**
 * 设置DinBold字体
 * @receiver TextView
 */
fun TextView?.setDinBold() {
  this?.typeface = dinBold
}

fun View.getVerticalLocationInScreen(): Int {
  val location = IntArray(2)
  this.getLocationOnScreen(location)
  return location[1]
}

fun View.getHorizontalLocationInScreen(): Int {
  val location = IntArray(2)
  this.getLocationOnScreen(location)
  return location[0]
}